package com.itheima.health.config;
/*
 *@description 
 com.sky.config
 *@create 2023/8/15 
*/


import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@Slf4j
public class RedisConfiguration {

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        log.info("开始创建redis模板对象..:{}", redisConnectionFactory);
        RedisTemplate redisTemplate = new RedisTemplate();
        // 制定连接工厂
        // 设置redis的连接工厂对象
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 设置redis key的序列化器（制定当前操作redis时key的序列方式、
        // 指定为字符串
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // value的序列化方式
        // redisTemplate.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
        // redisTemplate.setDefaultSerializer(new Jackson2JsonRedisSerializer(Object.class));
        // y有可能导致其他类型的数据出现问题
        redisTemplate.setDefaultSerializer(new FastJsonRedisSerializer<>(Object.class));
        return redisTemplate;
    }
}
